﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities;
using Forest.Core.Data;
using System.Data.SqlClient;
using System.Data.Objects.SqlClient;
using Forest.Cat.Domain.Shared;
using Forest.Cat.Domain.Configuration;

namespace Forest.Cat.Domain.Services.HomePages
{
    public partial class HomePageService : IHomePageService
    {
        private readonly IRepository<Advertisement> _repositoryAdvertisement;
        private readonly IRepository<Material> _repositoryMaterial;
        private readonly IRepository<Job> _repositoryJob;
        private readonly IRepository<JobPosting> _repositoryJobPosting;
        private readonly IRepository<Company> _repositoryCompany;
        private readonly IRepository<HotWord> _repositoryHotWord;

        public HomePageService(IRepository<Advertisement> repositoryAdvertisement, 
            IRepository<Material> repositoryMaterial,
            IRepository<Job> repositoryJob,
            IRepository<JobPosting> repositoryJobPosting,
            IRepository<Company> repositoryCompany,
            IRepository<HotWord> repositoryHotWord)
        {
            _repositoryAdvertisement = repositoryAdvertisement;
            _repositoryMaterial = repositoryMaterial;
            _repositoryJob = repositoryJob;
            _repositoryJobPosting = repositoryJobPosting;
            _repositoryCompany = repositoryCompany;
            _repositoryHotWord = repositoryHotWord;
        }

        public IList<Material> GetPublishedAdv(bool ispublished, int admode, int adtype, int areacode, int adcount)
        {
            var adlist = from a in _repositoryMaterial.Table
                         join b in _repositoryAdvertisement.Table
                         on a.Id equals b.MaterialId into c
                         from d in c.DefaultIfEmpty()
                         where d.IsPublished == ispublished
                         && a.AdMode == admode
                         && d.AreaCode == areacode
                         && a.AdType == adtype
                         && (SqlFunctions.DateDiff("dayofyear", d.StartDate, DateTime.Now) >= 0)
                         && (SqlFunctions.DateDiff("dayofyear", d.EndDate, DateTime.Now) <= 0)
                         orderby d.DisplayOrder
                         select a;

            return adlist.Take(adcount).ToList();
        }

        public IList<Company> GetHotJobCampany()
        {
            return null;
        }

        #region GetHotWord
        public IList<HotWord> GetHotWord(int count)
        {
            return _repositoryHotWord.Table.OrderByDescending(x => x.Weight).Take(count).ToList();
        }
        #endregion
    }
}
